<?php

class Fmusic_MusicController extends Core_Controller_Action_Standard
{
  public function init()
  {
 // Check auth
    if( !$this->_helper->requireAuth()->setAuthParams('music_playlist', null, 'view')->isValid()) {
      return;
    }

    // Get viewer info
    $this->view->viewer     = Engine_Api::_()->user()->getViewer();
    $this->view->viewer_id  = Engine_Api::_()->user()->getViewer()->getIdentity();

    // Get subject
    if( null !== ($playlist_id = $this->_getParam('playlist_id')) &&
        null !== ($playlist = Engine_Api::_()->getItem('music_playlist', $playlist_id)) &&
        $playlist instanceof Music_Model_Playlist &&
        !Engine_Api::_()->core()->hasSubject() ) {
      Engine_Api::_()->core()->setSubject($playlist);
    }
  }
   
public function viewlightboxAction()
{ 
	$this->view->status = true;
	if( !$this->_helper->requireSubject()->isValid() ) {
		$this->view->status = false;
	}
	
   // Get viewer/subject
    $viewer = Engine_Api::_()->user()->getViewer();
    $this->view->music = $music = Engine_Api::_()->core()->getSubject('music_playlist');

	    // Increment view count
    if( !$viewer->isSelf($music->getOwner()) ) {
      $music->view_count++;
      //$playlist->play_count++;
      $music->save();
    }
	
    // if this is sending a message id, the user is being directed from a coversation
    // check if member is part of the conversation
    $message_view = false;
    if( null !== ($message_id = $this->_getParam('message')) ) {
      $conversation = Engine_Api::_()->getItem('messages_conversation', $message_id);
      $message_view = $conversation->hasRecipient($viewer);
    }
    $this->view->message_view = $message_view;
	

    if( !$message_view && 
        !Engine_Api::_()->authorization()->isAllowed($music, $viewer, 'view') ) {
		$this->view->status = false;
	}


	$this->view->height = $this->_getParam('height', 500);
   	$this->view->widthLeft = $this->_getParam('widthLeft', 500);
   	$this->view->width = $this->_getParam('width', 1000); 
   	$this->view->canComment = $canComment =  Engine_Api::_()->core()->getSubject()->authorization()->isAllowed($viewer, 'comment'); 
	
	$this->view->owner_musics = $this->getOwnerMusics($music->owner_id);
	if( count($this->view->owner_musics) ){
		$this->view->next_music = $this->getOwnerMusicNext($music->owner_id, $music->getIdentity());
		$this->view->prev_music = $this->getOwnerMusicPrev($music->owner_id, $music->getIdentity());
		$this->view->first_music = $this->getFirstMusic($music->owner_id);
		$this->view->last_music = $this->getLastMusic($music->owner_id);
		
		$this->view->music_index = $this->getMusicIndex($music->owner_id, $music->getIdentity());
		$this->view->music_count = $this->getMusicsCount($music->owner_id);
	}	
  }
  

  public function quickEditAction(){ 
  		if( !$this->_helper->requireSubject()->isValid()) return;
    	if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'edit')->isValid() ) return;
  		
    	$title = $this->_getParam('title', '');
  		$description = $this->_getParam('description', '');
	  	$viewer = Engine_Api::_()->user()->getViewer();
	    $music = Engine_Api::_()->core()->getSubject();
	    // do edit
	    if($title != 'Write a title...')
	    	$music->title = $title;
	    if($description != 'Write a description...')	
	    	$music->description = $description;
	    $music->save();
			    		
        $this->view->status = true;
	    $this->view->message = 'Comment added';
	    $this->view->body = $this->view->action('infos', 'music', 'fmusic', array(
	      'playlist_id' => $this->_getParam('playlist_id'),
	      'format' => 'html',
	    ));
	    $this->_helper->contextSwitch->initContext();
  }
  public function infosAction(){
  	    if( !$this->_helper->requireSubject()->isValid()) return;
	    $viewer = Engine_Api::_()->user()->getViewer();
	    $this->view->music = $music = Engine_Api::_()->core()->getSubject();
		$this->view->can_edit = $can_edit = $music->isEditable();
		$this->view->can_delete = $can_delete = $music->isDeletable();
  }
  public function getOwnerMusics($owner_id){
	$table = Engine_Api::_()->getDbTable('playlists', 'music');
	return $table->fetchAll($table->select()->where('owner_id=?', $owner_id));
  }
  public function getOwnerMusicNext($owner_id, $current_playlist_id){
	$table = Engine_Api::_()->getDbTable('playlists', 'music');
	return $table->fetchRow($table->select()->where('owner_id=?', $owner_id)->where('playlist_id > ?', $current_playlist_id)->order('playlist_id ASC')->limit(1));
  }
  public function getOwnerMusicPrev($owner_id, $current_playlist_id){
	$table = Engine_Api::_()->getDbTable('playlists', 'music');
	return $table->fetchRow($table->select()->where('owner_id=?', $owner_id)->where('playlist_id < ?', $current_playlist_id)->order('playlist_id DESC')->limit(1));
  }
  public function getFirstMusic($owner_id){
	$table = Engine_Api::_()->getDbTable('playlists', 'music');
	return $table->fetchRow($table->select()->where('owner_id=?', $owner_id)->order('playlist_id ASC')->limit(1));
  }
  public function getLastMusic($owner_id){
	$table = Engine_Api::_()->getDbTable('playlists', 'music');
	return $table->fetchRow($table->select()->where('owner_id=?', $owner_id)->order('playlist_id DESC')->limit(1));
  }
  
  public function getMusicIndex($owner_id, $playlist_id)
  {
	$table = Engine_Api::_()->getDbTable('playlists', 'music');
    return $table
        ->select()
        ->from($table, new Zend_Db_Expr('COUNT(playlist_id)'))
        ->where('owner_id = ?', $owner_id)
        ->where('`playlist_id` > ?', $playlist_id)
        ->order('playlist_id ASC')
        ->limit(1)
        ->query()
        ->fetchColumn();
  }
  public function getMusicsCount($owner_id){
	$table = Engine_Api::_()->getDbTable('playlists', 'music');
    return $table
        ->select()
        ->from($table, new Zend_Db_Expr('COUNT(playlist_id)'))
        ->where('owner_id = ?', $owner_id)
        ->limit(1)
        ->query()
        ->fetchColumn();
  }
}